package com.leo.foodmaster.admin.customer.controller;

import com.leo.foodmaster.commons.entity.Result;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.leo.foodmaster.admin.customer.dto.CustomerDeliveryTypeDTO;
import com.leo.foodmaster.admin.customer.entity.CustomerDeliveryTypeEntity;
import com.leo.foodmaster.admin.customer.service.ICustomerDeliveryTypeService;
import com.leo.foodmaster.admin.customer.mapper.CustomerDeliveryTypeMapper;
import javax.annotation.Resource;

/**
 * 客户配送方式
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-11-20
 */
@RestController
@RequestMapping("/api/customer/deliveryType")
@Api(tags = "客户配送方式")
@Slf4j
public class CustomerDeliveryTypeController {
    @Resource
    private ICustomerDeliveryTypeService icustomerDeliveryTypeService;
    @Resource
    private CustomerDeliveryTypeMapper customerDeliveryTypeMapper ;


    @GetMapping("/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = "size", value = "每页显示记录数", paramType = "query",required = true, dataType="int")
    })
    public Result<IPage<CustomerDeliveryTypeDTO>> page(@ApiIgnore Page<CustomerDeliveryTypeDTO> page, @ApiIgnore CustomerDeliveryTypeDTO customerDeliveryTypeDTO){
    	IPage<CustomerDeliveryTypeDTO> pageList = icustomerDeliveryTypeService.pageList(page,customerDeliveryTypeDTO);
        return Result.success(pageList);
    }


    @GetMapping
    @ApiOperation("信息")
    public Result<CustomerDeliveryTypeDTO> get(@RequestParam Long id,@ApiIgnore CustomerDeliveryTypeDTO customerDeliveryTypeDTO){
    	CustomerDeliveryTypeEntity byId = icustomerDeliveryTypeService.getById(id);
    	if (BeanUtil.isEmpty(byId)) {
    		return Result.success();
		}
    	BeanUtil.copyProperties(byId, customerDeliveryTypeDTO);
    	return Result.success(customerDeliveryTypeDTO);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result<Boolean> save(@ApiIgnore @RequestBody CustomerDeliveryTypeDTO dto){
        CustomerDeliveryTypeEntity entity = new CustomerDeliveryTypeEntity();
        BeanUtil.copyProperties(dto,entity);
        Boolean save = icustomerDeliveryTypeService.save(entity);
        return  Result.success(save);
    }


    @PutMapping
    @ApiOperation("修改")
    public Result<Boolean> update(@ApiIgnore @RequestBody CustomerDeliveryTypeDTO dto){
        CustomerDeliveryTypeEntity entity = new CustomerDeliveryTypeEntity();
        BeanUtil.copyProperties(dto,entity);
        boolean updateById = icustomerDeliveryTypeService.updateById(entity);
        return  Result.success(updateById);
    }


    @DeleteMapping
    @ApiOperation("删除")
    public Result<Integer> delete(@RequestBody Integer[] ids){
    	List<Integer> asList = Arrays.asList(ids);
    	int deleteBatchIds = customerDeliveryTypeMapper.deleteBatchIds(asList);
    	return Result.success(deleteBatchIds);
    }

}
